package com.example.propanimationdemo.slice;

import com.example.propanimationdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;

public class MainAbilitySlice extends AbilitySlice {
    //创建属性动画对象
    AnimatorProperty animatorProperty = new AnimatorProperty();
    // 变量标记当前动画是否已经开启
    private boolean started = false;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取模板自动生成的Text组件
        Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
        // 设置属性动画所作用的组件
        animatorProperty.setTarget(text);
        // 设置循环执行的次数，此处设置为无限循环
        animatorProperty.setLoopedCount(AnimatorValue.INFINITE);
        // 设置动画执行的时长
        animatorProperty.setDuration(3000);

        // 设置动画效果，这里设置为旋转360度，同时透明度从0变到1，尺寸增大一倍
        animatorProperty.rotate(360).alphaFrom(0).alpha(1).scaleXFrom(1).scaleX(2).scaleYFrom(1).scaleY(2);

        //点击开始/停止动画
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!started) {
                    //开始动画
                    animatorProperty.start();
                } else {
                    //停止动画
                    animatorProperty.stop();
                }
                started = !started;
            }
        });
    }
}
